package com.java.service;

import com.java.dao.PointRuleDao;
import com.java.dao.ProductDao;
import com.java.dao.ProductLimitDao;
import com.java.vo.PointRuleVo;
import com.java.vo.ShopProduct;
import com.java.vo.ShopProductLimit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * Created by v.sc.d on 2017/4/12.
 */
@Service
public class ShopProductService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ProductDao productDao;

    @Autowired
    private ProductLimitDao productLimitDao;

    public ShopProduct getProductDetailById(int productId) {
        ShopProduct shopProduct = productDao.getShopProductById(productId);
        shopProduct.setShopProductLimit(productLimitDao.getShopProductByProductId(productId));
        return shopProduct;
    }

    public Integer getProductDetailCnt(String name, Integer isSend, String label, String content, Integer page) {
        Integer Cnt = productDao.getShopProductCnt(name, isSend, label, content, page);
        return Cnt;
    }

    public List<ShopProduct> getProductList(String name, Integer isSend, String label, String content, Integer page) {
        List<ShopProduct> shopProducts = productDao.getShopProductByAll(name, isSend, label, content, page);
        if (shopProducts != null) {
            for (ShopProduct shopProduct : shopProducts) {
                List<ShopProductLimit> shopProductByProductId = productLimitDao
                        .getShopProductByProductId(shopProduct.getId());
                shopProduct.setShopProductLimit(shopProductByProductId);
            }
        }
        return shopProducts;
    }

    public void formatProductLimit(List<ShopProductLimit> list) {
        int cnt = 0;
        int num = 0;
        for (ShopProductLimit shopProductLimit : list) {
            if (shopProductLimit.getLimitType() == 1 && shopProductLimit.getStatus() == 1) {
                cnt++;
            }
            if (shopProductLimit.getLimitType() == 0) {
                num = shopProductLimit.getLimitNum() < 9000 ? shopProductLimit.getLimitNum() : 0;
            }
        }
        for (ShopProductLimit shopProductLimit : list) {
            if (shopProductLimit.getLimitType() == 0) {
                shopProductLimit.setStatus(1);
            }
            if (cnt == 0) {
                if (shopProductLimit.getLimitType() == 1) {
                    shopProductLimit.setStatus(1);
                }
            }
        }
    }

    public List<ShopProduct> getProductActiveList() {
        List<ShopProduct> shopProducts = productDao.getProductActiveList();
        if (shopProducts != null) {
            for (ShopProduct shopProduct : shopProducts) {
                List<ShopProductLimit> shopProductByProductId = productLimitDao
                        .getShopProductByProductId(shopProduct.getId());
                shopProduct.setShopProductLimit(shopProductByProductId);
            }
        }
        return shopProducts;
    }

    @Transactional
    public boolean setShelf(int id, int isUpInt) {
        boolean setShelf = productDao.setShelf(id, isUpInt);
        if (0 == isUpInt && setShelf) {
            if (!productDao.cancelTemplateDetailByProduct(id)) {
                logger.info("商品下架 置灰专区失败");
            }
            /*
             * List<Map<String, Object>> checkProduct3IN1 =
             * productDao.checkProduct3IN1(id); if(checkProduct3IN1 != null){
             * for(int i = 0; i < checkProduct3IN1.size(); i++){ Map<String,
             * Object> map = checkProduct3IN1.get(i);
             * productDao.cancelTemplateDetail3IN1(
             * String.valueOf(map.get("detailGroupId")),String.valueOf(map.get(
             * "homeTemplateId"))); } }
             */
        }
        return setShelf;
    }

    @Transactional
    public Integer insertShopProduct(ShopProduct shopProduct) {
        int i = productDao.insertShopProduct(shopProduct);
        if (i != -1) {
            if (!CollectionUtils.isEmpty(shopProduct.getShopProductLimit())
                    && shopProduct.getShopProductLimit().size() != 0) {
                List<ShopProductLimit> shopProductLimits = shopProduct.getShopProductLimit();
                int cnt = 0;
                int num = 0;
                for (ShopProductLimit shopProductLimit : shopProductLimits) {
                    if (shopProductLimit.getLimitType() == 1 && shopProductLimit.getStatus() == 1) {
                        cnt++;
                    }
                    if (shopProductLimit.getLimitType() == 0) {
                        num = shopProductLimit.getLimitNum() < 9000 ? shopProductLimit.getLimitNum() : 0;
                    }
                }
                for (ShopProductLimit shopProductLimit : shopProductLimits) {
                    shopProductLimit.setShopProductId(String.valueOf(i));
                    if (shopProductLimit.getLimitType() == 0 && num == 0) {
                        shopProductLimit.setStatus(0);
                    }
                    if (cnt > 0 && cnt < 5) {
                        if (shopProductLimit.getLimitType() == 1) {
                            shopProductLimit.setLimitNum(num);
                        }
                        if (shopProductLimit.getLimitType() == 0) {
                            shopProductLimit.setStatus(0);
                        }
                    }
                    if (cnt == 5) {
                        if (shopProductLimit.getLimitType() == 1) {
                            shopProductLimit.setStatus(0);
                        }
                    }

                    productLimitDao.insertShopProductLimit(shopProductLimit);
                }
            }
            return i;
        } else {
            return -1;
        }
    }

    @Transactional
    public boolean updateShopProduct(ShopProduct shopProduct) {
        if (shopProduct.getId() == 0) {
            return false;
        }
        if (productDao.UpdateShopProduct(shopProduct) == -1) {
            return false;
        } else {
            int cnt = 0;
            int num = 0;
            for (ShopProductLimit shopProductLimit : shopProduct.getShopProductLimit()) {
                if (shopProductLimit.getLimitType() == 1 && shopProductLimit.getStatus() == 1) {
                    cnt++;
                }
                if (shopProductLimit.getLimitType() == 0) {
                    num = shopProductLimit.getLimitNum() < 9000 ? shopProductLimit.getLimitNum() : 0;
                }
            }
            for (ShopProductLimit shopProductLimit : shopProduct.getShopProductLimit()) {
                if (shopProductLimit.getLimitType() == 0 && num == 0) {
                    shopProductLimit.setStatus(0);
                }
                if (cnt > 0 && cnt < 5) {
                    if (shopProductLimit.getLimitType() == 1) {
                        shopProductLimit.setLimitNum(num);
                    }
                    if (shopProductLimit.getLimitType() == 0) {
                        shopProductLimit.setStatus(0);
                    }
                }
                if (cnt == 5) {
                    if (shopProductLimit.getLimitType() == 1) {
                        shopProductLimit.setStatus(0);
                    }
                }
                int flagNum = productLimitDao.UpdateShopProductLimit(shopProductLimit);
            }
            return true;
        }
    }

    @Autowired
    PointRuleDao ruleDao;

    public List<PointRuleVo> findAllPointRules() {
        List<PointRuleVo> allPointRules = ruleDao.findAllPointRules();

        PointRuleVo cashRule = new PointRuleVo();
        cashRule.setId(0);
        cashRule.setRuleName("现金");
        allPointRules.add(cashRule);
        return allPointRules;
    }
}
